<template>
  <q-dialog class="my_dialog" v-model="isFlagShow">
    <q-card class="my-card q-mx-lg">
      <q-toolbar class="row items-center my_form_header">
        <div class="my-tableTitle">
          {{dlgTitle}}
        </div>
        <q-space />
        <q-btn flat round dense icon="mdi-close" @click="close"/>
      </q-toolbar>
      <q-separator />
      <q-card-setion class="q-mx-sm">
        <q-scroll-area class="my-form-container">
          <div class="col q-gutter-xs">
            <q-item class="items-center">
              <q-item-label><span style="color: red">**</span>角色名称：</q-item-label>
              <q-item-section>
                <q-input
                  outlined
                  dense
                  v-model="v$.cname.$model"
                  @blur="v$.cname.$touch()"
                  :error="v$.cname.$error"
                  error-message="请输入不少于2个字符！"
                />
              </q-item-section>
            </q-item>
            <q-item class="items-center">
              <q-item-label><span style="color: red">**</span>权限字符：</q-item-label>
              <q-item-section>
                <q-input
                  outlined
                  dense
                  v-model="v$.key.$model"
                  @blur="v$.key.$touch()"
                  :error="v$.key.$error"
                  error-message="请输入不少于2个字符！"
                />
              </q-item-section>
            </q-item>
            <q-item class="items-center">
              <q-item-label>&nbsp;&nbsp;&nbsp;&nbsp;查询序码：</q-item-label>
              <q-item-section>
                <q-input
                  outlined
                  dense
                  v-model="param.sortCode"
                />
              </q-item-section>
            </q-item>
            <q-item class="items-center">
              <q-item-label>&nbsp;&nbsp;&nbsp;&nbsp;应用范围：</q-item-label>
              <q-item-section>
                <q-input
                  outlined
                  dense
                  type="number"
                  v-model="param.scope"
                />
              </q-item-section>
            </q-item>
          </div>
        </q-scroll-area>
      </q-card-setion>
      <q-separator />
      <div class="q-gutter-sm my_form_footer">
        <q-btn
          unelevated
          dense
          label="提交"
          type="submit"
          color="teal-3"
          @click="onSubmit"
          style="width: 80px"
        />
        <q-btn
          unelevated
          dense
          label="重置"
          type="reset"
          color="orange-3"
          @click="onReset"
          style="width: 80px"
        />
      </div>
    </q-card>
  </q-dialog>
</template>

<script lang="ts">
import { defineComponent, onMounted, ref, toRef, toRefs, PropType, watch } from 'vue';
import { isObjectEmpty } from '../../../util/utils';
import { useVuelidate } from '@vuelidate/core'
import { required, minLength } from '@vuelidate/validators'

import { RoleItem, RoleItemVO } from './RoleModel';

export default defineComponent({
  cname: 'RoleDlg',
  components: {
  },
  props: {
    itemData: {
      type: Object as PropType<RoleItem>,
      required: true
    }
  },
  emits: ['submit-role-item', 'cancel-role-item'],
  setup (props, context) {
    //数据解构
    const {
      itemData
    } = toRefs(props);
    //*******************************
    //      表单项
    //*******************************
    //窗口显示
    let isFlagShow = ref(false);

    //窗口标题
    let dlgTitle = ref('');

    //界面数据
    let param = ref<RoleItemVO>({
      id: 0,
      cname: '',
      key: '',
      sortCode: '',
      scope: 0
    });

    //关闭窗口
    const close = () => {
      isFlagShow.value = false;
      context.emit('cancel-role-item');
    }

    //显示窗口
    const show = (text: string) => {
      isFlagShow.value = true;
      dlgTitle.value = text;
    }

    //提交窗口
    const onSubmit = async () => {
      v$.value.$validate();
      if (v$.value.$error) {
        alert('输入参数校验错误!');
      } else {
        isFlagShow.value = false;
        let item: RoleItem = {
          id: param.value.id,
          cname: param.value.cname,
          key: param.value.key,
          sortCode: param.value.sortCode,
          scope: param.value.scope,
          createTime: '',
          createUserId: 0,
          updateTime: '',
          updateUserId: 0
        };

        //发送数据
        context.emit('submit-role-item', item);
      }
    }

    //重置
    const onReset = () => {
      if (param.value) {
        param.value.cname = '';
        param.value.key = '';
        param.value.sortCode = '';
        param.value.scope = 0;
      }

      //表单验证初始化
      v$.value.$reset();
    }

    //*******************************
    //      表单校验
    //*******************************
    //配置校验规则
    const rules = {
      id: {},
      cname: { required, minLength: minLength(2) },
      key: { required, minLength: minLength(2) },
      sortCode: {},
      scope: {}
    }

    //使用vuelidate，建立变量对应的校验规则
    const v$ = useVuelidate(rules, {
      id: toRef(param.value, 'id'),
      cname: toRef(param.value, 'cname'),
      key: toRef(param.value, 'key'),
      sortCode: toRef(param.value, 'sortCode'),
      scope: toRef(param.value, 'scope')
    });

    //*******************************
    //      启动模块
    //*******************************
    //初始化
    const init = () => {
      if (!isObjectEmpty(itemData.value)) {
        param.value.id = itemData.value.id;
        param.value.cname = itemData.value.cname;
        param.value.key = itemData.value.key;
        param.value.sortCode = itemData.value.sortCode;
        param.value.scope = itemData.value.scope;
      }

      //表单验证初始化
      v$.value.$reset();
    }

    //监听对象的所有属性
    watch(() => itemData, () => {
      init();
    }, { deep: true });

    //启动挂载组件
    onMounted(() => {
      init();
    })

    return {
      itemData,

      isFlagShow,
      dlgTitle,
      param,
      v$,
      init,
      close,
      show,
      onSubmit,
      onReset
    }
  }
})
</script>

<style lang="scss" scoped>
.my-card {
  alignment: center;
  width: 600px;
  max-width: 800px;
  .my-form-container {
    text-align: center;
    padding: 5px 50px 5px 50px;
    height: 300px;
  }
}
</style>
